package com.gzbugu.bugusys.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.hibernate.Session;
import org.hibernate.Transaction;

//import java.text.SimpleDateFormat ;

import com.opensymphony.xwork2.ActionSupport;
import com.gzbugu.bugusys.DTO.StaffDTO;
import com.gzbugu.bugusys.vo.Staff;
import com.gzbugu.bugusys.vo.StaffEducation;
import com.gzbugu.bugusys.vo.StaffCertificate;
import com.gzbugu.bugusys.vo.StaffExperience;
import com.gzbugu.bugusys.vo.StaffFamily;
import com.gzbugu.bugusys.util.HibernateUtils;

public class StaffRegisterAction extends ActionSupport implements RequestAware {

	private Map<String, Object> request;
	private StaffDTO staffDTO;
	Integer staffSex;
	StaffFamily staffFamily1, staffFamily2, staffFamily3;
	StaffEducation staffEducation1, staffEducation2, staffEducation3;
	StaffExperience staffExperience1, staffExperience2, staffExperience3;
	StaffCertificate staffCertificate1, staffCertificate2, staffCertificate3;

	public String execute() {
		Session session = null;
		Transaction tx = null;
		List errorList = new ArrayList();
		staffDTO.setError(errorList);
		String result = "";

		try {
			// 给session赋值并开启事务
			session = HibernateUtils.getSession();
			tx = session.beginTransaction();

			if (staffDTO.invalidate()) {

				// 将各个表格列属性赋值，首先是staffSex
				if (staffDTO.getStaffSexId() == 1) {
					staffSex = 2;
				} else {
					staffSex = 3;
				}

				// staff
				Staff staff = new Staff();
				staff.setStaffId(staffDTO.getStaffId());
				staff.setName(staffDTO.getStaffName());
				staff.setAddress(staffDTO.getStaffAddress());
				staff.setBirthday(staffDTO.getStaffBirthday());
				staff.setBirthplace(staffDTO.getStaffBirthplace());
				staff.setFormername(staffDTO.getStaffFormername());
				staff.setDepartment(staffDTO.getStaffDepartment());
				staff.setEducation(staffDTO.getStaffEducation());
				staff.setGraduateSchool(staffDTO.getStaffGraduateSchool());
				staff.setHiredate(staffDTO.getStaffHiredate());
				staff.setIdnumber(staffDTO.getStaffIdnumber());
				staff.setNation(staffDTO.getStaffNation());
				staff.setNativePlace(staffDTO.getStaffNativePlace());
				staff.setPoliticsStatus(staffDTO.getStaffPoliticsStatus());
				staff.setPost(staffDTO.getStaffPost());
				staff.setTel(staffDTO.getStaffTel());
				staff.setZip(staffDTO.getStaffZip());
				staff.setSex(staffSex);
				session.save(staff);

				// staffEducation
				if (staffDTO.getStaffEducationStart1() != null) {
					staffEducation1 = new StaffEducation();
					staffEducation1.setEnd(staffDTO.getStaffEducationEnd1());
					staffEducation1
							.setStart(staffDTO.getStaffEducationStart1());
					staffEducation1.setSchool(staffDTO
							.getStaffEducationSchool1());
					staffEducation1.setReference(staffDTO
							.getStaffEducationReference1());
					staffEducation1.setStaff(staff);
					session.save(staffEducation1);
				}
				if (staffDTO.getStaffEducationStart2() != null) {
					staffEducation2 = new StaffEducation();
					staffEducation2.setEnd(staffDTO.getStaffEducationEnd2());
					staffEducation2
							.setStart(staffDTO.getStaffEducationStart2());
					staffEducation2.setSchool(staffDTO
							.getStaffEducationSchool2());
					staffEducation2.setReference(staffDTO
							.getStaffEducationReference2());
					staffEducation2.setStaff(staff);
					session.save(staffEducation2);
				}
				if (staffDTO.getStaffEducationStart3() != null) {
					staffEducation3 = new StaffEducation();
					staffEducation3.setEnd(staffDTO.getStaffEducationEnd3());
					staffEducation3
							.setStart(staffDTO.getStaffEducationStart3());
					staffEducation3.setSchool(staffDTO
							.getStaffEducationSchool3());
					staffEducation3.setReference(staffDTO
							.getStaffEducationReference3());
					staffEducation3.setStaff(staff);
					session.save(staffEducation3);
				}

				// staffExperience
				if (staffDTO.getStaffExperienceStart1() != null) {
					staffExperience1 = new StaffExperience();
					staffExperience1.setStart(staffDTO
							.getStaffExperienceStart1());
					staffExperience1.setEnd(staffDTO.getStaffExperienceEnd1());
					staffExperience1
							.setPost(staffDTO.getStaffExperiencePost1());
					staffExperience1
							.setUnit(staffDTO.getStaffExperienceUnit1());
					staffExperience1.setStaff(staff);
					session.save(staffExperience1);
				}
				if (staffDTO.getStaffExperienceStart2() != null) {
					staffExperience2 = new StaffExperience();
					staffExperience2.setStart(staffDTO
							.getStaffExperienceStart2());
					staffExperience2.setEnd(staffDTO.getStaffExperienceEnd2());
					staffExperience2
							.setPost(staffDTO.getStaffExperiencePost2());
					staffExperience2
							.setUnit(staffDTO.getStaffExperienceUnit2());
					staffExperience2.setStaff(staff);
					session.save(staffExperience2);
					;
				}
				if (staffDTO.getStaffExperienceStart3() != null) {
					staffExperience3 = new StaffExperience();
					staffExperience3.setStart(staffDTO
							.getStaffExperienceStart3());
					staffExperience3.setEnd(staffDTO.getStaffExperienceEnd3());
					staffExperience3
							.setPost(staffDTO.getStaffExperiencePost3());
					staffExperience3
							.setUnit(staffDTO.getStaffExperienceUnit3());
					staffExperience3.setStaff(staff);
					session.save(staffExperience3);
				}

				// staffFamily
				if (staffDTO.getStaffFamilyName1() != null) {
					staffFamily1 = new StaffFamily();
					staffFamily1.setName(staffDTO.getStaffFamilyName1());
					staffFamily1.setRelationship(staffDTO
							.getStaffFamilyRelationship1());
					staffFamily1.setPost(staffDTO.getStaffFamilyPost1());
					staffFamily1.setUnit(staffDTO.getStaffFamilyUnit1());
					staffFamily1.setStaff(staff);
					session.save(staffFamily1);
				}
				if (staffDTO.getStaffFamilyName2() != null) {
					staffFamily2 = new StaffFamily();
					staffFamily2.setName(staffDTO.getStaffFamilyName2());
					staffFamily2.setRelationship(staffDTO
							.getStaffFamilyRelationship2());
					staffFamily2.setPost(staffDTO.getStaffFamilyPost2());
					staffFamily2.setUnit(staffDTO.getStaffFamilyUnit2());
					staffFamily2.setStaff(staff);
					session.save(staffFamily2);
				}
				if (staffDTO.getStaffFamilyName3() != null) {
					staffFamily3 = new StaffFamily();
					staffFamily3.setName(staffDTO.getStaffFamilyName3());
					staffFamily3.setRelationship(staffDTO
							.getStaffFamilyRelationship3());
					staffFamily3.setPost(staffDTO.getStaffFamilyPost3());
					staffFamily3.setUnit(staffDTO.getStaffFamilyUnit3());
					staffFamily3.setStaff(staff);
					session.save(staffFamily3);
				}

				// staffCertificate
				if (staffDTO.getStaffCertificateName1() != null) {
					staffCertificate1 = new StaffCertificate();
					staffCertificate1.setName(staffDTO
							.getStaffCertificateName1());
					staffCertificate1.setIssueDate(staffDTO
							.getStaffCertificateIssueDate1());
					staffCertificate1.setNotafter(staffDTO
							.getStaffCertificateNotafter1());
					staffCertificate1.setNotbefore(staffDTO
							.getStaffCertificateNotbefore1());
					staffCertificate1.setStaff(staff);
					session.save(staffCertificate1);
				}
				if (staffDTO.getStaffCertificateName2() != null) {
					staffCertificate2 = new StaffCertificate();
					staffCertificate2.setName(staffDTO
							.getStaffCertificateName2());
					staffCertificate2.setIssueDate(staffDTO
							.getStaffCertificateIssueDate2());
					staffCertificate2.setNotafter(staffDTO
							.getStaffCertificateNotafter2());
					staffCertificate2.setNotbefore(staffDTO
							.getStaffCertificateNotbefore2());
					staffCertificate2.setStaff(staff);
					session.save(staffCertificate2);
				}
				if (staffDTO.getStaffCertificateName3() != null) {
					staffCertificate3 = new StaffCertificate();
					staffCertificate3.setName(staffDTO
							.getStaffCertificateName3());
					staffCertificate3.setIssueDate(staffDTO
							.getStaffCertificateIssueDate3());
					staffCertificate3.setNotafter(staffDTO
							.getStaffCertificateNotafter3());
					staffCertificate3.setNotbefore(staffDTO
							.getStaffCertificateNotbefore3());
					staffCertificate3.setStaff(staff);
					session.save(staffCertificate3);
				}
				// 存储staff，因为级联关系其他表也会自动进行存储
				tx.commit();
				result = "success";
			} else {
				// 输出错误改变result
				request.put("errorList", errorList);
				result = "error";
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			tx.rollback();
		} finally {
			HibernateUtils.closeSession(session);
		}

		return result;

	}

	public StaffDTO getStaffDTO() {
		return staffDTO;
	}

	public void setStaffDTO(StaffDTO staffDTO) {
		this.staffDTO = staffDTO;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

}
